package com.newer.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newer.domain.Material;
import com.newer.domain.User;
import com.newer.dto.LoginDto;
import com.newer.dto.UserssDto;
import com.newer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("login")
@SessionAttributes("login")
public class LoginController {

        @Autowired
        private UserService userService;

        @RequestMapping("login")
        public UserssDto login(String name, String pwd, Model model, HttpSession session){


            List<LoginDto> login =  this.userService.selectNamePwd(name,pwd);


                if(login.size()!=0){
                    model.addAttribute("login",login.get(0));
                    return new UserssDto(true,login);
                }

            return new UserssDto(false,"用户名或者密码错误");

        }
        @RequestMapping("loginout")
        public UserssDto loginout(HttpSession session, SessionStatus sessionStatus){


            session.invalidate();
            sessionStatus.isComplete();

            return   new UserssDto(true,"退出登录");
        }
    //修改
    @RequestMapping("updateUser")
    public Boolean updateUser(@RequestBody User user){
        return this.userService.updateById(user);
    }

}
